#include "CSuccessorSearcher.h"
#include "astar/TIReceiver.h"

#include "map.h"

CSuccessorSearcher::CSuccessorSearcher()
: mXBound(sizeof(gMap[0]))
 ,mYBound(sizeof(gMap) / sizeof(gMap[0]))
{
}

CSuccessorSearcher::~CSuccessorSearcher()
{
}

bool CSuccessorSearcher::getNext(const key & k, TIReceiver<key> & receiver)
{
   int x = 0;
   int y = 0;
   Key2XY(k, x, y);
   add(receiver, x - 1    , y + 1);
   add(receiver, x - 1    , y);
   add(receiver, x - 1    , y - 1);
   add(receiver, x    , y + 1);
   add(receiver, x    , y - 1);
   add(receiver, x + 1, y + 1);
   add(receiver, x + 1, y);
   add(receiver, x + 1, y - 1);
   return true;
}

void CSuccessorSearcher::add(TIReceiver<key> & receiver, int iX, int iY) const
{
   if(iX >= 0 && iX <= mXBound && iY >= 0 && iX <= mYBound)
   {
      if(gMap[iX][iY] != '.')
      {
         key k = 0;
         XY2Key(iX, iY, k);
         receiver.add(k);
      }
   }
}